package com.jsu.springboot_crop.controller;

import com.jsu.springboot_crop.common.Result;
import com.jsu.springboot_crop.common.StatusCode;
import com.jsu.springboot_crop.entity.Finance;
import com.jsu.springboot_crop.entity.Intention;
import com.jsu.springboot_crop.entity.Recommend;
import com.jsu.springboot_crop.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wang
 * @Date 2023/8/25 19:43
 */
@RestController
@RequestMapping("/finance")
//@CrossOrigin
public class FinanceController {
    @Autowired
    FinanceService financeService;

    //添加单人贷款
    @PostMapping("/add")
    public Result addFinance(@RequestBody Finance finance) {
        //登录时用,获取用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        finance.setOwnName(name);
        financeService.addFinance(finance);
        return new Result(true, StatusCode.OK, "申请成功");
    }

    //添加组合贷款
    @PostMapping("/addFinanceMulti")
    public Result addFinanceMulti(@RequestBody Finance finance) {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        finance.setOwnName(name);
        financeService.addFinanceMulti(finance);
        return new Result(true, StatusCode.OK, "申请成功");
    }
    //查询融资情况
    @GetMapping("/selectByName")
    public Result selectByName() {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Finance finance = new Finance();
        finance.setOwnName(name);
        List<Finance> finances = financeService.selectByFinance(finance);
        return new Result(true, StatusCode.OK, "查询成功", finances);
    }
    //根据id查询融资情报
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        Finance finance = financeService.selectByFinanceId(id);
        return new Result(true, StatusCode.OK, "查询成功", finance);
    }
    //根据id修改融资情报
    @PutMapping("/update")
    public Result update(@RequestBody Finance finance) {
        financeService.updateByFinance(finance);
        return new Result(true, StatusCode.OK, "修改成功");
    }
    //根据id删除融资情报
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        financeService.deleteByFinanceId(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
    //查询融资意向
    @GetMapping("/selectIntentionByName")
    public Result selectIntentionByName() {
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        List<Intention> intentions = financeService.selectIntentionByName(name);
        return new Result(true, StatusCode.OK, "查询成功", intentions);
    }
    //修改融资意向
    @PutMapping("/updateIntentionByName")
    public Result updateIntentionByName(@RequestBody Intention intention) {
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        intention.setUserName(name);
        financeService.updateIntentionByName(intention);
        return new Result(true, StatusCode.OK, "修改成功");
    }
    //添加融资意向
    @PostMapping("/insertIntentionByName")
    public Result insertIntentionByName(@RequestBody Intention intention) {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        intention.setUserName(name);
        financeService.insertIntentionByName(intention);
        return new Result(true, StatusCode.OK, "添加成功");
    }
    //删除融资意向
    @DeleteMapping("/deleteIntentionByName")
    public Result deleteIntentionByName() {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        financeService.deleteIntentionByName(name);
        return new Result(true, StatusCode.OK, "删除成功");
    }
    //查询推荐融资人
    @GetMapping("/selectRecommendByName")
    public Result selectRecommendByName() {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        List<Recommend> recommends = financeService.selectRecommend(name);
        return new Result(true, StatusCode.OK, "查询成功", recommends);
    }

    //查询是否已经贷款
    @GetMapping("/selectIfApply")
    public Result selectIfApply() {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        boolean b = financeService.selectIfApply(name);
        return new Result(true, StatusCode.OK, "已经贷款", b);
    }
}
